package com.zz.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zz.entity.Manager;
import com.zz.entity.Ware;
import com.zz.service.WareService;
import com.zz.util.KeyUtil;

@RestController
@RequestMapping("ware")
public class WareController {
	@Resource
	WareService wareService;
	//分页查询所有
	@RequestMapping("findAll")
	public Page<Ware> findAll(HttpServletRequest request){
		String startpage=request.getParameter("start");
		System.out.println("startpage=="+startpage);
		return wareService.findAll(startpage, "5");
	}
	//修改
	@RequestMapping("findByid")
	public Optional<Ware> findByid(HttpServletRequest request) {
		String wid=request.getParameter("wid");
		System.out.println("wid==="+wid);
		return wareService.findById(wid);
	}
	@RequestMapping("update")
	public Ware update(HttpServletRequest request) {
		String id=request.getParameter("upwid");
		String name=request.getParameter("name");
		String address=request.getParameter("address");
		String person=request.getParameter("person");
		String tel=request.getParameter("tel");
		Ware ware=new Ware();
		ware.setId(id);
		ware.setName(name);
		ware.setPerson(person);
		ware.setAddress(address);
		ware.setTel(tel);
		return wareService.updateware(ware);
	}
	//删除
	@RequestMapping("delete")
	public Map deletewareById(HttpServletRequest request) {
		String id=request.getParameter("deletewid");
		boolean rs=false;
		Optional<Ware> h=wareService.findById(id);
		if(h!=null) {
			rs=true;
			wareService.deleteById(id);
		}else {
			rs=false;
		}
		Map map=new HashMap();
		map.put("deleteResult", rs);
		return map;
	}
	//查找
	@RequestMapping("findByName")
	public List<Ware> findByName(HttpServletRequest request){
		String name=request.getParameter("selectname");
		return wareService.findByName(name);
	}
	//添加
	@RequestMapping("insert")
	public Ware insertware(HttpServletRequest request) {
		String name=request.getParameter("insertname");
		String address=request.getParameter("insertaddress");
		String person=request.getParameter("insertperson");
		String tel=request.getParameter("inserttel");
		Ware ware=new Ware();
		ware.setId("CK"+KeyUtil.genUniqueKey());
		ware.setName(name);
		ware.setPerson(person);
		ware.setAddress(address);
		ware.setTel(tel);
		return wareService.saveware(ware);
	}
	@RequestMapping("checkwarename")
	public Map checkwarename(HttpServletRequest request) {
		String name=request.getParameter("insertname");
		boolean f=false;
		if(wareService.findwareByname(name)==null) {
			f=true;
		}else {
			f=false;
		}
		Map map=new HashMap();
		map.put("warenamef", f);
		return map;
	}
	//hello name
	@RequestMapping("helloname")
	public Map helloname(HttpServletRequest request) {
		HttpSession session=request.getSession();
		Manager m=(Manager) session.getAttribute("loginMana");
		Map map=new HashMap();
		map.put("loginMana", m);
		return map;
	}
}
